using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using WebUpdaterAdminCore.Data;
using WebUpdaterAdminCore.Models;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace WebUpdaterAdminCore.WebApi
{
    [Route("api/[controller]")]
    public class ClientGroupsController : Controller
    {
        private ApplicationDbContext _applicationDb;

        public ClientGroupsController(ApplicationDbContext db)
        {
            _applicationDb = db;
        }
        // GET: api/<controller>
        [HttpGet]
        public IActionResult Get(int pageNo = 1,int pageSize = -1,bool all = true)
        {
            pageNo = pageNo - 1 > 0 ? pageNo - 1 : 0;
            if (_applicationDb != null)
            {
                List<ClientGroup> list = null;
                if (all)
                {
                    list = _applicationDb.ClientGroups.ToList();
                }
                else
                {
                    list = _applicationDb.ClientGroups.Where(w => w.Useable).ToList();
                }
                var count = list.Count();
                int skipCount = pageNo * pageSize;
                var query = list.Skip(skipCount);
                if (pageSize > 0)
                {
                    query = query.Take(pageSize);
                }
                
                var data = new GetClientGroupsViewModel
                {
                    Total = count,
                    PageNo = pageNo,
                    PageSize = pageSize,
                    PageData = query
                };
                return Json(new OKResult<GetClientGroupsViewModel>(data));
            }
            else
            {
                return Json(new ErrorResult(100130, "无法访问数据库"));
            }
        }

        // GET api/<controller>/5
        [HttpGet("{id}")]
        public IActionResult Get(int id)
        {
            if (_applicationDb != null)
            {
                var data = _applicationDb.ClientGroups.FirstOrDefault(w => w.Id == id);
                if (data == null)
                {
                    return Json(new ErrorResult(100102, "对象不存在"));
                }
                return Json(new OKResult<ClientGroup>(data));
            }
            else
            {
                return Json(new ErrorResult(100130, "无法访问数据库"));
            }
        }

        // POST api/<controller>
        [HttpPost]
        public IActionResult Post([FromBody]ClientGroupViewModel model)
        {
            if (_applicationDb == null)
            {
                return Json(new ErrorResult(100130, "无法访问数据库"));
            }

            if (model != null && !string.IsNullOrWhiteSpace(model.GroupName))
            {
                var item = new ClientGroup
                {
                    Name = model.GroupName,
                    Useable = model.Useable,
                    Remarks = model.Remarks,
                    UpdateDate = DateTime.Now
                };
                _applicationDb.ClientGroups.Add(item);
                if (_applicationDb.SaveChanges() > 0)
                {
                    var newItem = _applicationDb.ClientGroups.LastOrDefault(w => w.Name == model.GroupName);
                    if(newItem != null)
                    {
                        return Json(new OKResult<ClientGroup>(newItem));
                    }
                    else
                    {
                        return Json(new ErrorResult(100103, "新增数据失败"));
                    }
                }
                else
                {
                    return Json(new ErrorResult(100103, "新增数据失败"));
                }
            }
            else
            {

                return Json(new ErrorResult(100101, "参数不能为空"));
            }
        }

        // PUT api/<controller>/5
        [HttpPut("{id}")]
        public IActionResult Put(int id, [FromBody]ClientGroupViewModel model)
        {
            if (_applicationDb == null)
            {
                return Json(new ErrorResult(100130, "无法访问数据库"));
            }
            if (model == null || string.IsNullOrWhiteSpace(model.GroupName))
            {
                return Json(new ErrorResult(100101, "参数不能为空"));
            }
            if(id != model.GroupId)
            {
                return Json(new ErrorResult(100110, "非法修改,请刷新重试"));
            }
            var item = _applicationDb.ClientGroups.Find(id);
            if(item == null)
            {
                return Json(new ErrorResult(100102, "对象不存在"));
            }
            item.Name = model.GroupName;
            item.Remarks = model.Remarks;
            item.Useable = model.Useable;
            item.UpdateDate = DateTime.Now;
            _applicationDb.ClientGroups.Update(item);
            if (_applicationDb.SaveChanges() > 0)
            {
                return Json(new OKResult());
            }
            else
            {
                return Json(new ErrorResult(100104, "修改数据失败"));
            }
        }

        // DELETE api/<controller>/5
        [HttpDelete("{id}")]
        public IActionResult Delete(int id)
        {
            if (_applicationDb == null)
            {
                return Json(new ErrorResult(100130, "无法访问数据库"));
            }
            var item = _applicationDb.ClientGroups.Find(id);
            if (item == null)
            {
                return Json(new ErrorResult(100102, "对象不存在"));
            }
            _applicationDb.ClientGroups.Remove(item);
            if (_applicationDb.SaveChanges() > 0)
            {
                return Json(new OKResult());
            }
            else
            {
                return Json(new ErrorResult(100105, "删除数据失败"));
            }
        }
    }
}
